<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
    <el-form-item label="部门" prop="depart">
          <!-- <el-input v-model="form.depart" placeholder="请选择部门" /> -->
          <!--                   @change="cascaderChange" -->
          <el-cascader
            placeholder="部门"
            v-model="queryParams.depart"
            :options="deptOptions"
            :props="{ value: 'label', multiple: false, emitPath: false,expandTrigger: 'hover' }"
            :show-all-levels="false"
            @change="cascaderChange22"
            filterable
          ></el-cascader>
        </el-form-item>
      <el-form-item label="培训时间" prop="time" label-width="120px">
        <el-date-picker
          clearable
          v-model="queryParams.beginTrainTime"
          type="datetime"
          value-format="yyyy-MM-dd HH:mm:ss"
          placeholder="请选择培训时间"
        >
        </el-date-picker>
      </el-form-item>
      <!-- <el-form-item label="带班结束时间" prop="time" label-width="120px">
        <el-date-picker
          clearable
          v-model="queryParams.endTrainTime"
          type="datetime"
          value-format="yyyy-MM-dd"
          placeholder="请选择带班日期"
        >
        </el-date-picker>
      </el-form-item> -->
   
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['app:meeting:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['app:meeting:edit']"
          >修改</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['app:meeting:remove']"
          >删除</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['app:meeting:export']"
          >导出</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="testhandleList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <!-- <el-table-column label="序号" align="center" prop="id" /> -->
      <el-table-column label="班组长" align="center" prop="classLeader" />
      <el-table-column label="培训时间" align="center" prop="trainTime" width="180">
        <!-- <template slot-scope="scope">
          <span>{{ parseTime(scope.row.time, '{y}-{m}-{d}') }}</span>
        </template> -->
      </el-table-column>
      <el-table-column label="部门" align="center" prop="depart" />
      <el-table-column label="培训地点" align="center" prop="trainPlace" />
      <el-table-column label="车间主任" align="center" prop="trainManager" />
      <el-table-column label="签到人员" align="center" prop="ous">
        <template slot-scope="scope">
          <el-button type="primary" @click="look(scope.row)">查看</el-button>
        </template>
      </el-table-column>
      <el-table-column label="培训图片" align="center" prop="trainPic" width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.trainPic" :width="50" :height="50" />
        </template>
      </el-table-column>
      <el-table-column label="培训内容" align="center" prop="remark" />
      <el-table-column
        label="操作"
        align="center"
        width="200px"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-check"
            @click="Signin(scope.row)"
            >签到</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['app:meeting:edit']"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['app:meeting:remove']"
            >删除</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-download"
            @click="handleExport"
            v-hasPermi="['app:meeting:export']"
            >导出</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改测试对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="部门" prop="depart">
          <!-- <el-input v-model="form.depart" placeholder="请选择部门" /> -->
          <!--                   @change="cascaderChange" -->
          <el-cascader
            placeholder="部门"
            v-model="form.depart"
            :options="deptOptions"
            :props="{ value: 'label', multiple: false, emitPath: false,expandTrigger: 'hover' }"
            :show-all-levels="false"
            @change="cascaderChange"
            filterable
          ></el-cascader>
        </el-form-item>

        <el-form-item label="班组长" prop="classLeader">
          <!-- <el-input v-model="form.classLeader" placeholder="请输入交接班情况" /> -->
          <el-select
            v-model="form.classLeader"
            collapse-tags
            filterable
            placeholder="选择人员"
          >
            <el-option
              v-for="item in userSelectList"
              :key="item.userId"
              :label="item.nickName"
              :value="item.nickName"
            ></el-option>
            <!-- userListSearch -->
          </el-select>
        </el-form-item>
        <el-form-item label="培训时间" prop="trainTime">
          <el-date-picker
            clearable
            v-model="form.trainTime"
            type="datetime"
            value-format="yyyy-MM-dd HH:mm:ss"
            placeholder="请选择培训日期"
            
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="培训地点" prop="trainPlace">
          <el-input v-model="form.trainPlace" placeholder="请输入培训地点" />
        </el-form-item>
        <el-form-item label="培训内容" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入培训内容" />
        </el-form-item>
        <el-form-item label="车间主任" prop="trainManager">
          <!-- <el-input v-model="form.trainManager" placeholder="请输入车间主任" /> -->
          <el-select
            v-model="form.trainManager"
            collapse-tags
            filterable
            placeholder="选择人员"
          >
            <el-option
              v-for="item in userSelectList"
              :key="item.userId"
              :label="item.nickName"
              :value="item.nickName"
            ></el-option>
            <!-- userListSearch -->
          </el-select>
          <!-- <image-upload v-model="form.trainManager" /> -->
        </el-form-item>
        <el-form-item label="培训图片" prop="trainPic">
          <!-- <el-input v-model="form.trainManager" placeholder="请输入车间主任" /> -->
          <ImageUpload v-model="form.trainPic" @input="upImage" :limit="1"></ImageUpload>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <!-- 签到人员弹框 -->
    <el-dialog
      title="签到人员"
      :visible.sync="humOpen"
      width="800px"
      append-to-body
    >
      <el-button type="primary" plain v-for="item in humData" :key="item.id"
        >{{ item.userName }}<br />{{ item.signTime }}</el-button
      >
      <!-- <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div> -->
    </el-dialog>
  </div>
</template>

<script>
import {
  listTesthandle,
  getTesthandle,
  delTesthandle,
  addTesthandle,
  updateTesthandle,
  
} from "@/api/app/testhandle";
import { deptTreeSelect, listUser } from "@/api/system/user";
import {
  meetingList,
  recordList,
  recordExport,
  meetingAdd,
  recordAdd,
  meetingEdit,
  meetingRemove
} from "@/api/system/bqh";
import cache from "@/plugins/cache";
export default {
  name: "Testhandle",
  data() {
    return {
      userId: cache.session.getJSON("userId"),
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 测试表格数据
      testhandleList: [],
      humData: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      humOpen: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: null,
        time: null,
        contenct: null,
        test: null,
        ous: null,
        flag: null,
        title: null,
        type: 1,
        beginTrainTime:null,
        endTrainTime:null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
      deptOptions: [],
      userSelectList: [],
    };
  },
  created() {
    this.getList();
    this.getDeptTree()
  },
  methods: {
    /** 查询测试列表 */
    getList() { 
      this.loading = true;
      meetingList({...this.queryParams}).then((response) => {
        this.testhandleList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    look(data) {
      this.humOpen = true;
      recordList({ meetingId: data.id }).then((res) => {
        console.log(res);
        this.humData = res.rows;
      });
    },
    upImage(e) {
      this.form.trainPic = e;
    },
    // 表单重置
    reset() {
      this.form = {
        classLeader: null,
        depart: null,
        remark: null,
        trainManager: null,
        trainPlace: null,
        trainTime: null,
        trainPic:null,
       
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;

      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      
      this.getUserList();
      this.reset();
      this.open = true;
      this.title = "添加班前会";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
     
      this.getUserList();
      this.reset();
      const id = row.id || this.ids;


      this.open = true;
      this.title = "修改班前会";
      this.form = {...row};
      // getTesthandle(id).then((response) => {
      //   this.form = response.data;
        
      // });
    },
    getCurrentTime() {
      const now = new Date(); 
      const year = now.getFullYear();
      const month = String(now.getMonth() + 1).padStart(2, "0");
      const day = String(now.getDate()).padStart(2, "0");
      const hours = String(now.getHours()).padStart(2, "0");
      const minutes = String(now.getMinutes()).padStart(2, "0");
      const seconds = String(now.getSeconds()).padStart(2, "0");
      let currentTime = `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;

      return currentTime
    },
    Signin(data){
      recordList({ meetingId: data.id }).then((res) => {
        console.log(res);
        for (let i = 0; i < res.rows.length; i++) {
          if(res.rows[i].userId == this.userId){
            this.$message.error('重复签到')
            return
          }
          
        }
        recordAdd({meetingId:data.id,userId:this.userId,signTime:this.getCurrentTime(),mineId:1}).then(res=>{
          console.log(res);
          this.$message({
          message: '签到成功',
          type: 'success'
        });
        })
      });
    },
    /** 查询部门下拉树结构 */
    getDeptTree() {
      deptTreeSelect().then((response) => {
        this.deptOptions = response.data;
      });
    },
    // 查询人员
    getUserList() {
      listUser({
        pageNum: 1,
        pageSize: 999,
      }).then((res) => {
        this.userSelectList = res.rows;
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
               meetingEdit(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            meetingAdd(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm('是否确认删除测试编号为"' + ids + '"的数据项？')
        .then(function () {
          return meetingRemove(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport(data) {
      // this.download(
      //   "app/testhandle/export",
      //   {
      //     ...this.queryParams,
      //   },
      //   `testhandle_${new Date().getTime()}.xlsx`
      // );
      recordExport({ meetingId: data.id }).then((res) => {
        console.log(res);
        if(res.success){
          window.open(res.data)
        }
      });
    },
    cascaderChange(data) {
      this.form.depart = data;
     

    },
    cascaderChange22(data) {
      this.queryParams.depart = data;
    },
    
  },
};
</script>
